package com.lh.api.controller.order;

import com.lh.api.annotation.Login;
import com.lh.api.annotation.NotRepeatRequest;
import com.lh.api.config.LHConfig;
import com.lh.api.controller.AbstractController;
import com.lh.api.form.OrderForm;
import com.lh.api.service.OrderService;
import com.lh.api.vo.*;
import com.lh.common.enums.OrderStatusEnum;
import com.lh.common.enums.OrderTypeEnum;
import com.lh.common.utils.AssertUtil;
import com.lh.common.utils.Constant;
import com.lh.common.utils.PageUtils;
import com.lh.common.utils.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Gze
 * @ClassName: OrderAction
 * @Description:
 * @date: 2020-05-18 15:21
 */

@Api(value="OrderAction",tags={"订单相关接口"})
@RestController
@RequestMapping("/api/order")
public class OrderAction extends AbstractController {

    @Autowired
    private OrderService orderService;

    @ApiOperation(value = "创建订单,返回数据中data：订单编号")
    @PostMapping("/createOrder")
    @Login
    public Result<CreateOrderVo> createOrder(@RequestBody @Validated OrderForm orderForm){
        Long userId = super.getUserId();
        if(OrderTypeEnum.ONEDOLLARRUSH.equals(orderForm.getOrderType())){
            AssertUtil.assertNotNull(orderForm.getPid(), Constant.RESULT.FI1000, "In OneDollarRush,pid is required");
            AssertUtil.assertNotNull(orderForm.getAid(), Constant.RESULT.FI1000, "In OneDollarRush,aid is required");
            AssertUtil.assertNotNull(orderForm.getNum(), Constant.RESULT.FI1000, "Number is required");
            AssertUtil.assertNotTrue(orderForm.getNum() <= 0, Constant.RESULT.FI1000, "Purchase quantity must be more than 0");
            AssertUtil.assertNotTrue(orderForm.getNum() > 20, Constant.RESULT.FI1000, "Maximum purchasing 20 at one time");
        }else {
            AssertUtil.assertNotNull(orderForm.getShopNums(), Constant.RESULT.FI1000, "ShopNums is required");
        }

        //创建订单，返回订单详情·
        CreateOrderVo createOrderVo = orderService.createOrder(userId, orderForm);
        return Result.ok(createOrderVo);
    }

    @ApiOperation(value = "一元夺宝订单列表")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="query", name = "pageNum", value = "当前页", required = true,dataTypeClass= Integer.class),
            @ApiImplicitParam(paramType="query", name = "pageSize", value = "页数大小", required = true,dataTypeClass= Integer.class),
            @ApiImplicitParam(paramType="query", name = "orderStatus", value = "订单状态,全部就不传，待付款：TOPAY，已完成：COMPLETED，已取消：CANCELLED", required = false, dataTypeClass = OrderStatusEnum.class)
    })
    @GetMapping("/getOneOrderList")
    @Login
    public Result<OneOrderListVo> getOneOrderList(Integer pageNum, Integer pageSize, OrderStatusEnum orderStatus) {
        pageNum = pageNum == null ? 1 : pageNum;
        pageSize = pageSize == null ? 10 : pageSize;

        Map<String, Object> params = new HashMap<>();
        params.put("userId", getUserId());
        if(orderStatus != null ) {
            params.put("orderStatus", orderStatus);
        }
        PageUtils<OneOrderListVo> resultList = orderService.getOneOrderList(params, pageNum, pageSize);
        return Result.ok(resultList);
    }

    @ApiOperation(value = "普通订单列表")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="query", name = "pageNum", value = "当前页", required = true,dataTypeClass= Integer.class),
            @ApiImplicitParam(paramType="query", name = "pageSize", value = "页数大小", required = true,dataTypeClass= Integer.class),
            @ApiImplicitParam(paramType="query", name = "orderStatus", value = "订单状态,全部就不传，TOPAY:待付款; TOSHIP:待发货; SHIPED:待收货;COMPLETED:已完成; CANCELLED:已取消", required = false, dataTypeClass = OrderStatusEnum.class)
    })
    @GetMapping("/getOrderList")
    @Login
    public Result<OrderListVo> getOrderList(Integer pageNum, Integer pageSize, OrderStatusEnum orderStatus) {
        pageNum = pageNum == null ? 1 : pageNum;
        pageSize = pageSize == null ? 10 : pageSize;

        Map<String, Object> params = new HashMap<>();
        params.put("userId", getUserId());
        if(orderStatus != null ) {
            params.put("orderStatus", orderStatus);
        }
        PageUtils<OrderListVo> resultList = orderService.getOrderList(params, pageNum, pageSize);
        return Result.ok(resultList);
    }

    @ApiOperation(value = "查询一元夺宝订单详情")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="query", name = "orderItem", value = "订单号", required = true,dataTypeClass= String.class)
    })
    @GetMapping("/getOneOrderDetail")
    @Login
    public Result<OneOrderDetailVo> getOneOrderDetail(String orderItem) {
        AssertUtil.assertNotNull(orderItem,Constant.RESULT.FI1000, Constant.ORDERITEM_REQUIRED);

        OneOrderDetailVo orderDetailVo = orderService.getOneOrderDetail(getUserId(), orderItem);
        return Result.ok(orderDetailVo);
    }

    @ApiOperation(value = "查询普通订单详情")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="query", name = "orderItem", value = "订单号", required = true,dataTypeClass= String.class)
    })
    @GetMapping("/getOrderDetail")
    @Login
    public Result<OrderDetailVo> getOrderDetail(String orderItem) {
        AssertUtil.assertNotNull(orderItem,Constant.RESULT.FI1000, Constant.ORDERITEM_REQUIRED);

        OrderDetailVo orderDetailVo = orderService.getOrderDetail(getUserId(), orderItem);
        return Result.ok(orderDetailVo);
    }

    @ApiOperation(value = "取消订单")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="query", name = "orderItem", value = "订单id", required = true,dataTypeClass= String.class)
    })
    @GetMapping("/cancelOrder")
    @NotRepeatRequest
    @Login
    public Result<String> cancelOrder(String orderItem){
        AssertUtil.assertNotNull(orderItem, Constant.RESULT.FI1000, "orderId is required");
        orderService.cancelOrder(orderItem, getUserId());
        return Result.ok();
    }


    @ApiOperation(value = "确认收货")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="query", name = "orderItem", value = "订单编号", required = true,dataTypeClass= String.class)
    })
    @GetMapping("/confirmOrder")
    @Login
    public Result<String> confirmOrder(String orderItem){
        AssertUtil.assertNotNull(orderItem, Constant.RESULT.FI1000, Constant.ORDERITEM_REQUIRED);

        orderService.confirmOrder(orderItem, getUserId());
        return Result.ok();

    }

    @ApiOperation(value = "删除订单记录")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="query", name = "orderItem", value = "订单编号", required = true,dataTypeClass= String.class)
    })
    @GetMapping("/delOrder")
    @Login
    public Result<String> delOrder(String orderItem){
        AssertUtil.assertNotNull(orderItem, Constant.RESULT.FI1000, Constant.ORDERITEM_REQUIRED);

        orderService.delOrder(orderItem, getUserId());
        return Result.ok();
    }
}
